<?php

/**
 * This is the model class for table "tbl_bidservice".
 *
 * The followings are the available columns in table 'tbl_bidservice':
 * @property integer $id
 * @property integer $user_id
 * @property integer $service_id
 * @property string $require
 * @property string $contact
 * @property string $timepost
 * @property string $timeupdate
 * @property integer $bid_contracted
 * @property integer $bid_deposited
 * @property integer $bid_completed
 * @property integer $service_contracted
 * @property integer $service_deposited
 * @property integer $service_completed
 * @property integer $dnd_paid
 */
class BidService extends CActiveRecord
{
    public $money_bid;
    public $data_complete;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return BidService the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_bidservice';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('require, contact', 'required'),
			array('user_id, service_id, bid_contracted, bid_deposited, bid_completed, service_contracted, service_deposited, service_completed, dnd_paid', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, service_id, require, contact, timepost, timeupdate, bid_contracted, bid_deposited, bid_completed, service_contracted, service_deposited, service_completed, dnd_paid', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'service' => array(self::BELONGS_TO, 'Service', 'service_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'Người làm',
			'service_id' => 'Dịch vụ',
			'require' => 'Require',
			'contact' => 'Contact',
			'timepost' => 'Timepost',
			'timeupdate' => 'Timeupdate',
			'bid_contracted' => 'Hợp đồng',
			'bid_deposited' => 'Đặt cọc',
			'bid_completed' => 'Kết thúc',
			'service_contracted' => 'Service Contracted',
			'service_deposited' => 'Service Deposited',
			'service_completed' => 'Service Completed',
			'dnd_paid' => 'Dnd Paid',
            'money_bid'=>'Tiền thuê (VNĐ)',
            'date_complete'=>'Hoàn thành (Ngày)',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('service_id',$this->service_id);
		$criteria->compare('require',$this->require,true);
		$criteria->compare('contact',$this->contact,true);
		$criteria->compare('timepost',$this->timepost,true);
		$criteria->compare('timeupdate',$this->timeupdate,true);
		$criteria->compare('bid_contracted',$this->bid_contracted);
		$criteria->compare('bid_deposited',$this->bid_deposited);
		$criteria->compare('bid_completed',$this->bid_completed);
		$criteria->compare('service_contracted',$this->service_contracted);
		$criteria->compare('service_deposited',$this->service_deposited);
		$criteria->compare('service_completed',$this->service_completed);
		$criteria->compare('dnd_paid',$this->dnd_paid);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
     public function search_user(){
    		// Warning: Please modify the following code to remove attributes that
    		// should not be searched.  
            
    		$criteria=new CDbCriteria;
            $criteria->condition='user_id='.Yii::app()->user->id;
            $criteria->order='bid_deposited DESC';
            $criteria->compare('service_id',$this->service_id,true);
    		$criteria->compare('bid_contracted',$this->bid_contracted);
    		$criteria->compare('bid_deposited',$this->bid_deposited);
    		$criteria->compare('bid_completed',$this->bid_completed);
    		$criteria->compare('service_contracted',$this->service_contracted);
    		$criteria->compare('service_deposited',$this->service_deposited);
    		$criteria->compare('service_completed',$this->service_completed);
    		$criteria->compare('dnd_paid',$this->dnd_paid);
            //$criteria->compare('countBid',$this->countBid);
    
    		return new CActiveDataProvider($this, array(
    			'criteria'=>$criteria,
                'pagination'=>array('pageSize'=>5),
    		));
    	}
    
    static function countBid($service_id){
             $countBidService = BidService::model()->countByAttributes(array('service_id'=>$service_id));
             return $countBidService;
    }
}